package bjtu.cit.bookshop.controller.admin;

import bjtu.cit.bookshop.controller.BaseController;
import bjtu.cit.bookshop.entities.Book;
import bjtu.cit.bookshop.entities.Order;
import bjtu.cit.bookshop.entities.OrderBook;
import bjtu.cit.bookshop.service.IBookService;
import bjtu.cit.bookshop.service.IOrderBookService;
import bjtu.cit.bookshop.service.IOrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jia19 on 2015/10/21.
 */
@Controller
@RequestMapping("/admin/")
public class OrderController extends BaseController {

    @Resource
    private IOrderService orderService = null;

    @Resource
    private IOrderBookService orderBookService = null;

    @Resource
    private IBookService bookService = null;

    /**
     * 显示订单列表
     * @param session
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("order.do")
    public String order(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "/admin/index";
        }

        String page = request.getParameter("page");

        if (page == null) {
            page = "1";
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "order");

        List<Order> orderList = orderService.getOrderList(10, (Integer.valueOf(page) - 1) * 10);
        model.addAttribute("orderList", orderList);
        if (orderList == null) {
            orderList = new ArrayList<Order>();
        }
        int len = orderList.size();
        model.addAttribute("totalPage", len / 10 + (len % 10 == 0?0:1));
        model.addAttribute("thisPage", page);

        return "/admin/order";
    }


    /**
     * 显示订单详情页面
     * @param session
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("orderInfo.do")
    public String orderDetail(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "/admin/index";
        }

        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "order");

        String id = request.getParameter("id");
        try {
            Order order = orderService.getOrderById(Integer.valueOf(id));
            List<OrderBook> books = orderService.getBookListByOrderId(order.getId());
            List<Book> bookList = new ArrayList<Book>();
            List<Integer> numberList = new ArrayList<Integer>();
            for (OrderBook orderBook : books) {
                bookList.add(bookService.getBookById(orderBook.getBookid()));
                numberList.add(orderBook.getNum());
            }

            model.addAttribute("order", order);
            model.addAttribute("bookList", bookList);
            model.addAttribute("numberList", numberList);

            return "admin/orderinfo";
        }
        catch (Exception e) {
            e.printStackTrace();
            return "/admin/badParameter";
        }
    }

    @RequestMapping("changeOrderState.do")
    public String changeOrderState(HttpSession session, HttpServletRequest request) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "redirect:/admin/index.do";
        }
        String str_id = request.getParameter("id");
        String state = request.getParameter("state");

        try {
            int id = Integer.parseInt(str_id);
            Order order = orderService.getOrderById(id);
            order.setState(state);
            orderService.updateOrder(order);
            return "redirect:/admin/order.do";
        }
        catch (Exception e) {
            e.printStackTrace();
            return "badParameter";
        }
    }

    /**
     * 内部类，用于封装订单内的图书列表
     */
    class OrderBookDetail
    {
        private String book;

        public OrderBook getOrderBook() {
            return orderBook;
        }

        public void setOrderBook(OrderBook orderBook) {
            this.orderBook = orderBook;
        }

        private OrderBook orderBook;

        public String getBook() {
            return book;
        }

        public void setBook(String book) {
            this.book = book;
        }
    }

}
